package com.thinkcube.edutainment.fragment;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import android.app.Fragment;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.thinkcube.edutainment.R;
@EFragment(R.layout.vedio_fragment)
public class VideoFragment extends Fragment implements OnPreparedListener,OnErrorListener{
	
	@ViewById(R.id.vidio)
	VideoView videoView;
	
	@ViewById(R.id.course_title)
	TextView coursetitle;
	
	@ViewById(R.id.backtolesson)
	Button backTolesson;
	
	@ViewById(R.id.progressBar)
	ProgressBar progress;
	
	@ViewById(R.id.notification_msg)
	TextView noPreview;
	
	@AfterViews
	void showVideoLesson()
	{
		if(getArguments().containsKey("Url")){
			String path=getArguments().getString("Url");
			
			if(getArguments().containsKey("Preview"))
			{
				if(getArguments().getBoolean("Preview"))
				{
					backTolesson.setText("Close");
					coursetitle.setVisibility(View.GONE);
				}
			}
			
			if(getArguments().containsKey("title"))
			{
				coursetitle.setVisibility(View.VISIBLE);
				coursetitle.setText(getArguments().getString("title"));
			}
		
			if(path.equals("false"))
			{
				progress.setVisibility(View.INVISIBLE);
				noPreview.setVisibility(View.VISIBLE);
			}
			else
			{
				Uri uri=Uri.parse(path);
				videoView.setVideoURI(uri);
				MediaController mc = new MediaController(getActivity());
				videoView.setMediaController(mc);  
				mc.setPadding(250, 0, 0, 0);
				videoView.requestFocus();
				
				videoView.setOnPreparedListener(this);
				videoView.setOnErrorListener(this);
			}
			
			//videoView.start();
		}
	}
	
	@Click(R.id.backtolesson)
	void onBackClick(){
		getFragmentManager().popBackStack();		
	}

	@Override
	public void onPrepared(MediaPlayer mp) {
		progress.setVisibility(View.INVISIBLE);
		mp.start();
	}

	@Override
	public boolean onError(MediaPlayer mp, int what, int extra) {
		progress.setVisibility(View.INVISIBLE);
		noPreview.setVisibility(View.VISIBLE);
		return false;
	}
	
	
	

}
